<?php
/**
 * 
 * Plugins class file
 * 
 * @author		Stijn Van Minnebruggen
 * @version		1.0.0
 * 
 */

class Plugins
{
	
	/**
	 * Main variable with all the plugins
	 * 
	 * @var		array
	 */
	
	private $_plugins = array();
	
	
	/**
	 * Main object with all the plugin instances
	 * 
	 * @var		object
	 */
	
	private $_objects = array();
	
	
	/**
	 * Constructor function
	 * 
	 * @return	void
	 */
	
	public function __construct()
	{
		global $cosa;
		if($cosa->system('installed')) $this->_plugins = $this->_getInstalled();
	}
	
	
	/**
	 * Get installed plugins
	 * 
	 * @return	array
	 */
	
	private function _getInstalled()
	{
		// required
			global $cosa, $db;
			$plugins = array();
		
		// fetch plugins
			$sql = "SELECT id, name, path FROM ".$db->table('plugins')." ORDER BY name";
			$res = mysql_query($sql) or $cosa->kill('Could not fetch plugins.', 'db');
		
		// return result
			while($row = mysql_fetch_array($res, MYSQL_ASSOC)) $plugins[stripslashes($row['path'])] = $row;
			return $plugins;
		
	}
	
	
	/**
	 * Create html header
	 * 
	 * @return	string
	 */
	
	public function htmlHeader()
	{
		// required
			global $cosa;
			$html = '';
		
		// generate html headers
			foreach($this->_plugins as $path => $plugin) {
				$plug_path = ROOT_PATH.$cosa->system('plugins-path').'/'.$path.'/plugin.php';
				if(file_exists($plug_path)) {
					
					// activate plugin
						include $plug_path;
						$pl = new $path();
					
					// html header
						$pluginHtml = (method_exists($pl, 'htmlHeader')) ? $pl->htmlHeader() : '';
						$html .= $pluginHtml."\n\t";
					
					// save plugin object
						$this->_objects[$path] = $pl;
					
				}
			}
		
		// return value
			return $html;
		
	}
	
	
	/**
	 * Create html footer
	 * 
	 * @return	string
	 */
	
	public function htmlFooter()
	{
		// required
			global $cosa;
			$html = '';
		
		// generate html footers
			if(!$this->_objects) return $html;
			foreach($this->_objects as $path => $plugin) {
				$pluginHtml = (method_exists($plugin, 'htmlFooter')) ? $plugin->htmlFooter() : '';
				$html .= $pluginHtml."\n\t";
			}
		
		// return value
			return $html;
		
	}
	
	
}


?>